//package com.md.infrastructure.config;
//
//import com.md.util.system.Snowflake;
//import com.md.value.server.ServerContext;
//import lombok.NonNull;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.core.Ordered;
//import org.springframework.stereotype.Component;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
///**
// * 处理数据的过滤器
// *
// * @author 刘骄阳
// * 2022-04-06 17:14
// */
//@Component
//@Slf4j
//@RequiredArgsConstructor
//public class ServerContextFilter extends OncePerRequestFilter implements Ordered {
//  private final Snowflake snowflake;
//
//
//  /**
//   * Same contract as for {@code doFilter}, but guaranteed to be
//   * just invoked once per request within a single request thread.
//   * See {@link #shouldNotFilterAsyncDispatch()} for details.
//   * <p>Provides HttpServletRequest and HttpServletResponse arguments instead of the
//   * default ServletRequest and ServletResponse ones.
//   *
//   * @param request     request
//   * @param response    response
//   * @param filterChain filterChain
//   */
//  @Override
//  protected void doFilterInternal(final @NonNull HttpServletRequest request,
//                                  final @NonNull HttpServletResponse response,
//                                  final @NonNull FilterChain filterChain) throws ServletException, IOException {
//    log.info("数据过滤器执行~~~");
////    String parentId = request.getHeader(ServerContextConstants.DATA_ID);
////    String rootId = request.getHeader(ServerContextConstants.ROOT_ID);
////    if (StringUtils.isNotBlank(parentId)) {
////      ServerContext.put(ServerContextConstants.PARENT_ID, parentId);
////    }
////    String dataId = String.valueOf(snowflake.nextId());
////    if (StringUtils.isBlank(rootId)) {
////      rootId = dataId;
////
////    }
////    ServerContext.put(ServerContextConstants.DATA_ID, dataId);
////    ServerContext.put(ServerContextConstants.ROOT_ID, rootId);
////    //  判断接口是否需要权限
////    // 不需要token直接放行
////    String token = request.getHeader(ServerContextConstants.TOKEN);
////    if (StringUtils.isNotBlank(token)) {
////      // 如果token不存在,返回需要token
////      ServerContext.put(ServerContextConstants.TOKEN, token);
////      Claims claims = EncryptionUtils.jwtDecrypt(token);
////      log.info("用户信息为: {}", StringUtil.objectToJson(claims));
////      Users users = StringUtil.jsonToObject(String.valueOf(claims.get("users")), Users.class);
////      if (users != null) {
////        ServerContext.put(ServerContextConstants.USER_ID, users.getId());
////      }
////    }
////    // 获取访问地址
////    ServerContext.put(ServerContextConstants.URL, request.getRequestURL().toString());
////    String ipAddress = HttpUtils.getIpAddress(request);
////    ServerContext.put(ServerContextConstants.IP_ADDRESS, ipAddress);
////    log.info(ipAddress);
//    filterChain.doFilter(request, response);
//    ServerContext.clear();
//  }
//
//  /**
//   * 获取此对象的顺序值,值越低优先级越高
//   *
//   * @return the order value
//   */
//  @Override
//  public int getOrder() {
//    return 8;
//  }
//
//}
